package com.xgc.book.bookserver.controller.api;

import com.xgc.book.bookserver.bean.*;
import com.xgc.book.bookserver.controller.BaseLoginController;
import com.xgc.book.bookserver.service.*;
import com.xgc.book.bookserver.util.ResultJson;
import com.xgc.book.bookserver.util.form.CommentForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassNameApiVipController
 * @Author:王润之
 * @Description
 * @DateTime2023/11/29 19:52
 **/
@RestController
@RequestMapping(value = {"/api/vip", "/wx/vip"})
public class ApiVipController extends BaseLoginController {
    @Autowired
    private VipService vipService;
    @Autowired
    private BookCommentService bookCommentService;
    @Autowired
    private BookLikeService bookLikeService;
    @Autowired
    private RecordService recordService;
    @Autowired
    private BookCatalogService bookCatalogService;
    @Autowired
    private FeedbackService feedbackService;

    @GetMapping("/info")
    public ResultJson info() {
        Integer id = getLoginId();
        VipBean vipBean = vipService.getById(id);
        return ResultJson.ok(vipBean);
    }

    @PostMapping("/save")
    public ResultJson save(@RequestBody VipBean vipBean) {
        vipService.saveOrUpdate(vipBean);
        return ResultJson.ok();
    }

    @PostMapping("/logout")
    public ResultJson logout() {
        return vipService.logout(getLoginId());
    }

    @PostMapping("/comment/save")
    public ResultJson saveComment(@RequestBody CommentForm commentForm) {
        commentForm.setVipId(getLoginId());
        vipService.saveComment(commentForm);
        return ResultJson.ok();
    }

    @DeleteMapping("/comment/delete/{id}")
    public ResultJson deleteComment(@PathVariable int id) {
        bookCommentService.removeById(id);
        return ResultJson.ok();
    }

    @GetMapping("/like/check")
    public ResultJson isLike(Integer bookId) {
        if (getLoginId() == null) {
            return ResultJson.ok(false);
        }
        if (bookId == null) {
            return ResultJson.error("图书编号异常！");
        }
        return ResultJson.ok(bookLikeService.isLike(bookId, getLoginId()));
    }

    @PostMapping("/like/save")
    public ResultJson updateLike(@RequestBody BookLikeBean bookLikeBean) {
        bookLikeBean.setVipId(getLoginId());
        bookLikeService.updateLike(bookLikeBean);
        return ResultJson.ok();
    }

    @GetMapping("/catalog/book/{bookId}")
    public ResultJson getCatalogId(@PathVariable int bookId) {
        return ResultJson.ok(recordService.getRecord(bookId, getLoginId()));
    }

    @PostMapping("/record/save")
    public ResultJson saveRecord(@RequestBody RecordBean recordBean) {
        recordBean.setVipId(getLoginId());
        return recordService.savaRecord(recordBean);
    }

    @GetMapping("/catalog/{id}")
    public ResultJson catalogInfo(@PathVariable int id) {
        return bookCatalogService.getCatalogById(id);
    }

    @GetMapping("/like/page")
    public ResultJson likePage(int page, int limit) {
        BookLikeBean bookLikeBean = new BookLikeBean();
        bookLikeBean.setVipId(getLoginId());
        return bookLikeService.getPage(page, limit, bookLikeBean);
    }

    @GetMapping("/record/page")
    public ResultJson recordPage(int page, int limit) {
        RecordBean recordBean = new RecordBean();
        recordBean.setVipId(getLoginId());
        return recordService.getPage(page, limit, recordBean);
    }

    @DeleteMapping("/record/delete/{id}")
    public ResultJson deleteRecord(@PathVariable int id) {
        recordService.removeById(id);
        return ResultJson.ok();
    }

    @GetMapping("/comment/page")
    public ResultJson commentPage(int page, int limit) {
        BookCommentBean bookCommentBean = new BookCommentBean();
        bookCommentBean.setVipId(getLoginId());
        return bookCommentService.getPage(page, limit, bookCommentBean);
    }

    @GetMapping("/feedback/page")
    public ResultJson feedbackPage(int page, int limit) {
        FeedbackBean feedbackBean = new FeedbackBean();
        feedbackBean.setUserId(getLoginId());
        return feedbackService.getPage(page, limit, feedbackBean);
    }

    @PostMapping("/feedback/save")
    public ResultJson feedbackSave(@RequestBody FeedbackBean feedbackBean) {
        if (feedbackBean.getAnonymous() == 1) {
            feedbackBean.setUserId(null);
        } else {
            feedbackBean.setUserId(getLoginId());
        }
        feedbackService.save(feedbackBean);
        return ResultJson.ok();
    }
}

